package edu.nf.vac.api.controller;

import cn.hutool.core.bean.BeanUtil;
import edu.nf.vac.api.vo.ReservationVO;
import edu.nf.vac.api.vo.ResultVO;
import edu.nf.vac.reservation.dto.ReservationDTO;
import edu.nf.vac.reservation.inf.ReservationService;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;

import java.util.List;

/**
 * @author LQJ
 * @date 2022/3/31
 */
public class ReservationController extends BaseController{
    @DubboReference(interfaceClass = ReservationService.class,
    version = "1.0.0",check = false)
    private ReservationService reservationService;
    @GetMapping("/reservations")
    @ApiOperation(value = "查询个人预约记录",httpMethod = "GET")
    @ApiResponses({
        @ApiResponse(code = 200, message = "返回个人预约记录", response = ResultVO.class),
        @ApiResponse(code = 500, message = "服务异常")
    })
    public ResultVO<List<ReservationVO>> listReservation(@RequestAttribute("uid") Integer uid){
        List<ReservationDTO> dtoList = reservationService.listReservationByUid(uid);
        List<ReservationVO> voList = BeanUtil.copyToList(dtoList, ReservationVO.class);
        return success(voList);
    }
    @PostMapping("/reservation")
    @ApiOperation(value="创建预约", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="reservationVO", value="预约信息", required=true, dataTypeClass= ReservationVO.class, paramType="body"),
            @ApiImplicitParam(name="nid", value="针次ID", required=true, dataTypeClass= Integer.class, paramType="body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功", response = ResultVO.class),
            @ApiResponse(code = 500, message = "创建失败")
    })
    public ResultVO addReservation(ReservationVO reservationVO, Integer nid, @RequestAttribute("uid") Integer uid) {
        ReservationDTO reservationDTO = BeanUtil.copyProperties(reservationVO, ReservationDTO.class);
        reservationDTO.setUid(uid);
        reservationDTO.setNid(nid);
        reservationService.addReservation(reservationDTO);
        return success();
    }


}
